<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="上传供应商回执合同" v-model="visible" align-center width="500px" :z-index="2000" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140">
      <el-form-item label="上传文件" >
        <FileUpload v-model="form.oss_url" :fileType="['pdf','excel']" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import FileUpload from '@/components/FileUpload'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({oss_url: []})

// 打开弹窗
const open = async (c_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = {c_id, oss_url: []}
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async () => {
  if(!form.value.oss_url?.length) {
    ElMessage.error('上传文件不能为空')
    return
  }
  loadings.create = true
  const {c_id, oss_url} = form.value
  api.upload({c_id, oss_url: oss_url[0]}).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>